<?php
/**
 * alltosun.com  push_helper.php
 * ============================================================================
 * 版权所有 (C) 2009-2013 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 王德康 (wangdk@alltosun.com) $
 * $Date: 2015-11-29 下午4:42:17 $
 * $Id: push_helper.php 183850 2016-04-30 07:56:33Z wangdk $
 */

class push_helper
{
    /**
     * 获取用户的极光设备标示
     * @param unknown $user_ids
     * @return array()
     */
    public static function get_device_ids($user_ids)
    {
        if (!$user_ids) {
            return false;
        }

        $reg_ids = _model('user_device')->getFields('device_id', array('user_id'=>$user_ids));
        return $reg_ids;
    }

    /**
     * 保存用户的指定客户端注册id
     * 如果该用户有设备号，那么都要清空
     * @param unknown $user_id
     * @param unknown $reg_id
     * @param unknown $platform ios = 1 android=2
     * @return string|array
     */
    public static function save_user_reg_id($user_id, $reg_id, $platform)
    {
        # @wangdk 2016-04-30
        $back_list_config = array('18171adc030e33b9c8a', '160a3797c806eab4f55', '170976fa8a83bc2a841');
        #$back_list_config = array('170976fa8a83bc2a841');
        if (in_array($reg_id, $back_list_config)) {
            return false;
        }

        if (!$reg_id || !$platform) return false;

        $table_name   = 'user_device';
        $platform     = strtolower($platform);

        if ($platform == 'ios') {
            $platform = 1;

        } else if ($platform == 'android') {
            $platform = 2;

        } else if ($platform == 'h5') {
            $platform = 3;

        } else {
            $platform = 0;
        }

        $user_info = _uri($table_name, array('user_id'=>$user_id));
        if (!$user_info) {

            $data['user_id']        = $user_id;
            $data['device_source']  = $platform;
            $data['device_id']      =  $reg_id;

            return _model($table_name)->create($data);
        } else {
            return _model($table_name)->update($user_info['id'], array('device_source'=>$platform, 'device_id'=>$reg_id));
        }

        return false;
    }

}
?>
